(function() {
	angular
		.module('ydApp.check', [])
		.config(function($stateProvider) {
			$stateProvider.state('add-check', {
				cache: false,
				url: '/add-check:id',
				controller: "addCheckCtrl",
				templateUrl: 'views/check/add-check.html'
			});
			$stateProvider.state('edit-check', {
				cache: false,
				url: '/edit-check:id',
				controller: "editCheckCtrl",
				templateUrl: 'views/check/edit-check.html'
			});
			$stateProvider.state('add-check-succ', {
				cache: false,
				url: '/add-check-succ',
				controller: "addCheckSuccCtrl",
				templateUrl: 'views/check/add-check-succ.html'
			});
		})
		.controller("addCheckCtrl", addCheckCtrl)
		.controller("editCheckCtrl", editCheckCtrl)
		.controller("addCheckSuccCtrl", addCheckSuccCtrl);

	function addCheckCtrl(xhr, $scope, $state, $window, localStore, $ionicLoading, Cameranav, fileTransfer, popup, $stateParams, $ionicModal) {

		var vm = $scope.vm = {};

		$scope.downloadImg = function(img) {
			return config.api + "HealthInquiry" + img;
		}

		vm.model = {};
		vm.ipData = {}
		$scope.showImages = function(index) {
			vm.ipData.showImage(index);
		}

		var user = localStore.get("user");

		vm.model.userId = user.id;

		var addcheck = localStore.get("addcheck");

		if (addcheck) {
			vm.model = addcheck;
		}

		if ($stateParams.id) {
			vm.isDetail = true;
			vm.title = "体检鉴定详情";
		} else {
			vm.title = "新增体检鉴定";
		}

		if (user.branchId) {
			vm.model.branchId = user.branchId;
			vm.model.branchName = user.branchName;
		} else {
			vm.model.branchName = "选择科室";
		}

		if (!$stateParams.id) {
			if (user.docId) {
				vm.model.doctorUserId = user.docId;
				vm.model.doctorName = user.docName;
				xhr.get('HealthInquiry/com/appraisal/selectCallCost', {
					id: user.docId
				}).then(function(result) {
					vm.model.apprCost = result.data.appraisalCost;
				});
			} else {
				vm.model.doctorName = "选择医生";
			}
		}

		if (vm.isDetail) {
			xhr.get('HealthInquiry/com/appraisal/findById', {
				id: $stateParams.id
			}).then(function(result) {
				vm.model = result.data;
				vm.ipData.images = vm.model.appraisalPictures;
				// var pisc = result.data.appraisalPictures[0].split(',');
				// result.data.appraisalPictures = pisc;
				// vm.model = result.data;
				vm.imgWidth = vm.model.appraisalPictures.length * 64;
				// console.log(JSON.stringify(vm.model))
			});
		}
		vm.select = function(params) {
			if (vm.isDetail) {
				return;
			}

			var user = localStore.save("addcheck", vm.model);

			$state.go(params, {
				state: 'add-check',
				branchId: localStore.get("user").branchId
			});
		}

		vm.submit = function() {
			if (vm.model.appraisalPictures.length == 0) {
				popup.alert('请选择图片');
				return;
			}
			// var pics = '';
			// for (var i = 0; i < vm.model.appraisalPictures.length; i++) {
			// 	pics+= 'appraisalPictures='+vm.model.appraisalPictures[i]+'&';
			// }
			// vm.model.appraisalPictures = undefined;
			// vm.model.appraisalPictures = pics;
			// console.log(JSON.stringify(vm.model))
			xhr.post('HealthInquiry/com/appraisal/saveOrUpdateJson', vm.model).then(function(result) {
				localStore.del('addcheck');
				$state.go('add-check-succ');
			}, function(err) {
				console.log(JSON.stringify(err))
			});
		}

		$scope.getAvatar = function() {
			if (vm.isDetail || vm.model.appraisalPictures.length >= 10) {
				return;
			}
			Cameranav.getPicture(function(imgUri) {
					$ionicLoading.show({
						template: '正在上传…'
					});
					fileTransfer.upload(imgUri, user.id, function(res) {
						$ionicLoading.hide();
						if (res.successful) {
							vm.model.appraisalPictures.push(res.data.picUrl);
							vm.ipData.images = vm.model.appraisalPictures;
							vm.imgWidth = vm.model.appraisalPictures.length * 64;
						}
					});
				})
				//这里为图片上传成功后服务器返回的url
		}

		$scope.delAvatar = function(index) {
			if (vm.isDetail) {
				return;
			}
			//长按删除图片
			vm.model.appraisalPictures.splice(index, 1);
			vm.ipData.images = vm.model.appraisalPictures;
			vm.imgWidth = vm.model.appraisalPictures.length * 64;
		}

		vm.imgWidth = 0;
		//vm.model.appraisalPictures = ['img/temp1.jpg', 'img/temp1.jpg', 'img/temp1.jpg', 'img/temp1.jpg', 'img/temp1.jpg', 'img/temp1.jpg']
		vm.model.appraisalPictures = [];
		vm.imgWidth = vm.model.appraisalPictures.length * 64;

	}

	function editCheckCtrl(xhr, $scope, $state, $window, localStore, $ionicLoading, Cameranav, fileTransfer, popup, $stateParams) {

		var vm = $scope.vm = {};

		$scope.downloadImg = function(img) {
			return config.api + "HealthInquiry" + img;
		}

		var editcheck = localStore.get("editcheck");

		var user = localStore.get("user");

		vm.model = {};
		vm.ipData = {}
		$scope.showImages = function(index) {
			vm.ipData.showImage(index);
		}

		if (editcheck && editcheck.apprState == 0 && user.branchId) {
			editcheck.branchId = user.branchId;
			xhr.get('HealthInquiry/com/appraisal/refuseAppraisal', editcheck).then(function(result) {
				popup.alert(result.resultMsg);
				localStore.del("editcheck");
				$window.history.back();
				//$state.go('tabs.recent-check');
				return;
			});
		}

		xhr.get('HealthInquiry/com/appraisal/findById', {
			id: $stateParams.id
		}).then(function(result) {
			vm.model = result.data;
			vm.ipData.images = vm.model.appraisalPictures;
			//vm.model.appraisalPictures[0].
			//alert(typeof(vm.model.appraisalPictures[0]))
			// console.log(JSON.stringify(result.data))
			localStore.del("editcheck");
			vm.imgWidth = vm.model.appraisalPictures.length * 64;
		});

		vm.submit = function() {
			xhr.get('HealthInquiry/com/appraisal/doAppraisal', vm.model).then(function(result) {
				//$state.go('tabs.recent-check');
				$window.history.back();
			});
		}

		// vm.notidentify = function() {
		// 	localStore.save("editcheck", vm.model);
		// 	$state.go('register-doc-info-depart', {
		// 		state: 'edit-check'
		// 	});
		// };

		vm.notidentify = function() {
			popup.confirm('此报告是否无法鉴定,推荐其他科室?').then(function(res) {
				if (res) {
					localStore.save("editcheck", vm.model);
					$state.go('register-doc-info-depart', {
						state: 'edit-check'
					});
				}
			});
		}

		vm.imgWidth = 0;
		// vm.model.appraisalPictures = ['img/temp1.jpg', 'img/temp1.jpg', 'img/temp1.jpg', 'img/temp1.jpg', 'img/temp1.jpg', 'img/temp1.jpg']
		// vm.imgWidth = vm.model.appraisalPictures.length * 64;
	}

	function addCheckSuccCtrl($state, $timeout, $window) {
		$timeout(function() {
			$window.history.go(-2);
			//$state.go('tabs.recent-check');
		}, 2000)
	}
})();